﻿using System;
using System.Collections.Generic;
using System.Linq;
using NodeEditorFramework;
using UnityEngine;
using MainScene.Evt.Command;
using MainScene.Evt.Condition;
using UnityEditor;
using MainScene;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using NodeEditorFramework.Standard;

[NodeCanvasType("LevelMap Canvas")]
public class LevelMapNodeCanvas : NodeCanvas
{
    [NonSerialized]
    public const string levelMapCanvasPath = "Assets/ClientRes/SceneLevelMap";
    [NonSerialized]
    public const string levelMapBundlePath = "Assets/OutputBundle/SceneConfig";

    public string RuntimeConfig
    {
        get
        {
            return extraParma;//这里extraParma 用来做xml数据路径
        }
        set
        {
            extraParma = value;
        }
    }
    public override string canvasName { get { return "LevelMap"; } }
    public override void DrawCanvasPropertyEditor()
    {
        base.DrawCanvasPropertyEditor();
    }
    public override NodeEditorInterface GetNodeEditorInterface()
    {
        return new NodeEditorInterfaceEx();
    }
    protected override void OnCreate()
    {
        base.OnCreate();
        if (nodeSubCanvas == null)
        {
            nodeSubCanvas = new LevelMapSubCanvas();
        }
    }
    public override void UpdateSource(string path, string extraParma)
    {
        base.UpdateSource(path, extraParma);
        this.extraParma = extraParma;
    }
    public override void DrawCanvasSubEditor()
    {
        if (nodeSubCanvas != null)
        {
            nodeSubCanvas.DrawSubCanvas();
        }
        else
        {
            nodeSubCanvas = new LevelMapSubCanvas();
            nodeSubCanvas.DrawSubCanvas();
        }
    }

    public override void OnBeforeSavingCanvas()
    {
        base.OnBeforeSavingCanvas();
        List<SceneEventData> datas = new List<SceneEventData>();
        StoryNode temp;
        for (int i = 0; i < nodes.Count; i++)
        {
            temp = nodes[i] as StoryNode;
            datas.Add(temp.events);
        }
        string filePath = Application.dataPath.Substring(0, Application.dataPath.Length - 7) + "/" + extraParma;
        Stream stream;

        if (File.Exists(filePath))
        {
            stream = File.Open(filePath, FileMode.Open);
        }
        else
        {
            stream = File.Open(filePath, FileMode.Create);
        }
        SceneConfig sceneConfig;
        //先把数据读出来
        XmlSerializer serializer = new XmlSerializer(typeof(SceneConfig));
        try
        {
            sceneConfig = (SceneConfig)serializer.Deserialize(stream);
            //将事件部分替换
            if (sceneConfig != null)
            {
                sceneConfig.defaultEventList = datas;
            }
            stream.Close();
            //然后再从新存储
            Stream sw = File.Open(filePath, FileMode.OpenOrCreate);
            XmlWriterSettings xms = new XmlWriterSettings();
            xms.Indent = true;
            xms.Encoding = System.Text.Encoding.UTF8;
            XmlWriter xmlWriter = XmlWriter.Create(sw, xms);
            XmlSerializer serializer0 = new XmlSerializer(typeof(SceneConfig));
            serializer0.Serialize(xmlWriter, sceneConfig);
            sw.Close();
        }
        catch (Exception e)
        {
            Debug.LogError("xml bundle 数据保存失败 -> " + e.Message);
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }
    }
}
